package net.zio.kamenriderzioaddon.network;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.AABB;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraft.world.phys.Vec3;

import net.zio.kamenriderzioaddon.KamenRiderZioAddonMod;
import net.zio.kamenriderzioaddon.network.AddonMapVariables;
import net.zio.kamenriderzioaddon.ridersystem.HenshinManager;
import net.zio.kamenriderzioaddon.ridersystem.data.PlayerDataManager;
import net.zio.kamenriderzioaddon.ridersystem.registry.RiderRegistry;

import java.util.ArrayList;

/**
 * 事件总线处理器
 *
 * @author QianYunYi
 */
@Mod.EventBusSubscriber(
        modid = KamenRiderZioAddonMod.MODID,
        bus = Mod.EventBusSubscriber.Bus.MOD // 明确指定 MOD 总线
)
public final class AddonEventBusHandlers {

    // 全局变身管理器实例
    public static final HenshinManager HENSHIN_MANAGER = new HenshinManager();

    /**
     * 服务器启动时注册所有骑士配置
     */
    @SubscribeEvent
    public static void onCommonSetup(FMLCommonSetupEvent event) {
        // 在服务器启动时注册所有骑士
        event.enqueueWork(() -> {
            RiderRegistry.registerAllRiders();
            System.out.println("[Kamen Rider Zio] 所有骑士配置已成功注册！");
        });
    }
}

@Mod.EventBusSubscriber(
        modid = KamenRiderZioAddonMod.MODID,
        bus = Mod.EventBusSubscriber.Bus.FORGE // 处理 FORGE 总线事件
)
class ForgeEventBusHandler {

    /**
     * 玩家登录事件
     */
    @SubscribeEvent
    public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
        Player player = event.getPlayer();
        if (!player.level.isClientSide()) {
            syncPlayerVar(player);
            AddonMapVariables.get(player.level).syncData(player.level);
            AddonWorldData.get(player.level).sync(player.level);
        }
    }

    /**
     * 玩家重生事件
     */
    @SubscribeEvent
    public static void onPlayerRespawned(PlayerEvent.PlayerRespawnEvent event) {
        if (!event.getPlayer().level.isClientSide) {
            syncPlayerVar(event.getPlayer());
        }
    }

    /**
     * 玩家切换维度事件
     */
    @SubscribeEvent
    public static void onPlayerChangedDimension(PlayerEvent.PlayerChangedDimensionEvent event) {
        if (!event.getPlayer().level.isClientSide) {
            AddonWorldData.get(event.getPlayer().level).sync(event.getPlayer().level);
        }
    }

    /**
     * 玩家克隆事件
     */
    @SubscribeEvent
    public static void onClonePlayer(PlayerEvent.Clone event) {
        if (event.isWasDeath() && !event.getEntity().level.isClientSide) {
            PlayerDataManager old = PlayerDataManager.get(event.getOriginal());
            PlayerDataManager neu = PlayerDataManager.get(event.getPlayer());
            neu.deserializeNBT(old.serializeNBT());
            neu.sync(event.getPlayer());
        }
    }

    /**
     * 同步玩家变量
     */
    private static void syncPlayerVar(Entity player) {
        Level level = player.level;
        if (!level.isClientSide) {
            for (Entity e : new ArrayList<>(level.getEntities(null, AABB.ofSize(Vec3.ZERO, Double.MAX_VALUE, Double.MAX_VALUE, Double.MAX_VALUE)))) {
                if (e instanceof ServerPlayer sp) {
                    PlayerDataManager.get(sp).sync(sp);
                }
            }
        }
    }
}